<?php

namespace app\store\model;

use think\Cache;
use app\common\model\Category as CategoryModel;

/**
 * 商品分类模型
 * Class Category
 * @package app\store\model
 */
class Category extends CategoryModel
{
    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        $data['wxapp_id'] = self::$wxapp_id;
        if ($data['parent_id']){
            $tem_arr = explode('.',$data['name']);
            if (count($tem_arr)<1 || count($tem_arr)==1){
                $this->error = '在填写章节分类时请按照“第几章/节+点+章/节名称”格式填写，如：第一章.基础入门';
                return false;
            }
        }
        $this->deleteCache();

        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if ($data['parent_id']){
            $tem_arr = explode('.',$data['name']);
            if (count($tem_arr)<1 || count($tem_arr)==1){
                $this->error = '在填写章节分类时请按照“第几章/节+点+章/节名称”格式填写，如：第一章.基础入门';
                return false;
            }
        }
        $this->deleteCache();
        !array_key_exists('image_id', $data) && $data['image_id'] = 0;
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 删除商品分类
     * @param $categoryId
     * @return bool|int
     */
    public function remove($categoryId)
    {
        //获取所有子分类
        $child_arr = static::getSubCategoryId($categoryId);
        //删除所有内容数据
        $this->deletecontent($child_arr);
        //删除缓存文件
        $this->deleteCache();
        return $this->whereIn('category_id',$child_arr)->delete();
    }

    private function deletecontent($categoryIdArr){
        $lesson = new Lesson();
        $lessondetail = new LessonDetails();
        //判断分类下是否存在内容
        foreach ($categoryIdArr as $k=>$v){
            $content = $lesson->where('category_id','=',$v)->find();
            if($content){
                $lessondetail->where('lesson_id','=',$content['lesson_id'])->delete();
                $content->delete($content['lesson_id']);
            }
        }
    }

    /**
     * 删除缓存
     * @return bool
     */
    private function deleteCache()
    {
        return Cache::rm('category_' . static::$wxapp_id);
    }

    /**
     * 获取课程总量
     */
    public function getCategoryTotal(){
        return $this->where('parent_id','=',0)->count();
    }

    /**
     * 计算课程内容总数量
     * @return int|string
     * @throws \think\Exception
     */
    public function getLessonTotal(){
        return $this->where('isused','<>',0)->count();
    }
    /**
     *计算新增课程数量
     */
    public function getCategoryTotalDay($days, $daye){
        $days = strtotime($days." 00:00:00");
        $daye = strtotime($daye." 23:59:59");
        $condition = $days .','. $daye;
        return $this->where('create_time','between',$condition)
            ->where('parent_id','=',0)
            ->count();
    }
    /**
     * 计算新增课程内容总数量
     * @return int|string
     * @throws \think\Exception
     */
    public function getLessonTotalDay($days, $daye){
        $days = strtotime($days." 00:00:00");
        $daye = strtotime($daye." 23:59:59");
        $condition = $days .','. $daye;
        return $this->where('update_time','between',$condition)
            ->where('isused','<>',0)
            ->count();
    }

}
